<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="150px"
    >
      <el-form-item label="用户昵称" prop="nickName">
        <el-select
          v-model="dataForm.nickName"
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option
            v-for="item in options"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="通知内容" prop="noticeContent">
        <el-input
          v-model="dataForm.noticeContent"
          placeholder="noticeContent"
        ></el-input>
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="dataForm.remark" placeholder="remark"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: ["options"],
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        nickName: "",
        noticeContent: "",
        remark: "",
      },
      dataRule: {
        nickName: [
          { required: true, message: "用户昵称不能为空", trigger: "blur" },
        ],
        noticeContent: [
          { required: true, message: "账期不能为空", trigger: "blur" },
        ],
        remark: [
          { required: true, message: "基本薪资不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (id) {
          this.dataForm.id = id.id;
          this.dataForm.nickName = id.notice_pub_id;
          this.dataForm.noticeContent = id.notice_content;
          this.dataForm.remark = id.remark;
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/business/enotice/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id,
              noticePubId: this.dataForm.nickName,
              noticeContent: this.dataForm.noticeContent,
              remark: this.dataForm.remark,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
